<?php
	require '../../include/library.inc.php';
	require '../../include/admin_login.inc.php';
	
	unset($_SESSION['album_id']);
			
	if (!isset($_POST['is_submitted'])) {
		$_POST['is_submitted'] = '';
	}
	if (!isset($error_message)) {
		$error_message = '';
	}	
	
	
	if($_POST["redirect"] || $_SESSION['redirect'] ){
		if($_POST["redirect"]) $redirect = $_POST["redirect"];
		if($_SESSION['redirect']){ 
			$redirect = $_SESSION['redirect'];
			unset($_SESSION['redirect']);
		}
	}else{
		   $redirect = $_SERVER['HTTP_REFERER'];	
		 }	
	
	
	if ($_POST['is_submitted']) {
		$error_message = '';
		$redirect = $_POST["redirect"];
    
	$sql = 'SELECT lang_id,Name,code FROM language WHERE published';
	$result_lang = query($sql);
	
	while ($row_lang = mysql_fetch_object($result_lang)) {
				$em = $row_lang->lang_id;
				eval('$name =  $_POST[\'title_'.$em.'\'];');
				if (!$name)  $error_message .= 'Не е въведено заглавие на '.$row_lang->Name.'<br/>';
				
	}
	
	
	
	if (!$_POST['sef_url'])  $error_message .= 'Не е въведен SEF URL<br/>';
		elseif(!preg_match('/^[0-9A-Za-z\p{Cyrillic}\-\_]+$/u', $_POST['sef_url'])) $error_message .= 'Въведеният SEF URL е невалиден<br/>';
		
	$sql = "SELECT
				album_id
			FROM
				album
			WHERE
				sef_url = '".$_POST['sef_url']."'
			  ";
	$result = query($sql);
	if ($row_seo = mysql_fetch_object($result)) $error_message .= 'Този SEF URL вече е използван! <br/>';
	

	//ОК
    if(!$error_message) {
		
		
		
		$sql = 'SELECT MAX(order_number)+1 AS order_number FROM album';
		$row_number = mysql_fetch_assoc(query($sql));
		
		//Създаваме проекта
		$sql = 'INSERT INTO album
				  (sef_url,  published, order_number)
				VALUES
				  (\''.escape($_POST['sef_url']).'\', \''.intval($_POST['published']).'\', \''.$row_number['order_number'].'\')
				  ';
		query($sql);
		
		
		$id = mysql_insert_id();
		$sql = 'SELECT lang_id,Name,code
				FROM
				language
				WHERE
				published
					';
		$result_lang = query($sql);
		
		while ($row_lang = mysql_fetch_object($result_lang)) {
			
				$em = $row_lang->lang_id;
				
				eval('$name =  $_POST[\'title_'.$em.'\'];');
				eval('$description =  $_POST[\'description_'.$em.'\'];');
				
			
			$sql = "INSERT INTO album_description
				  (album_id, lang_id, title, description)
				VALUES
				  ('".$id."','".$em."', '".escape($name)."', '".escape($description)."')";
				 
		query($sql);
		}
		
		
		
		// Връзка с категория
		if($_POST['category_id'])
		{
			$category_id = $_POST['category_id'];
		  
			foreach ($category_id as $key => $value) {
			
				$sql = 'INSERT INTO album_to_category
							  (album_id,category_id)
							VALUES
							  ('.$id.','.intval($value).')
							  ';
				query($sql);
			
			}
		}
		
		// Връзка с фотограф
		if($_POST['photographer_id'])
		{
			$photographer_id = $_POST['photographer_id'];
		  
			foreach ($photographer_id as $key => $value) {
			
				$sql = 'INSERT INTO album_to_photographers
							  (album_id,photographer_id)
							VALUES
							  ('.$id.','.$value.')
							  ';
				query($sql);
			
			}
		}

		
		
		$_SESSION['message'] .= 'Албумът е създаден успешно!';
        redirect('album_edit.php?album_id='.$id);
		 
        
    }

}
  	
	head_admin("Добавяне на нов албум");
?>





	<?php if ($error_message) { ?>

	<div class="message red">
	<h3>Грешка!</h3>
	<?=$error_message?>
	</div>
	<?php } ?>
    
     <?php if ($_SESSION['message']) { ?>
			<div class="message green">
				
				<?=$_SESSION['message']?>
			</div>
		<?php 
            unset($_SESSION['message']);
        } ?>
            
    <div class="topbuttons"> <a href="<?=$redirect?>">Обратно</a> </div>
    <h3>Добавяне на нов албум</h3>
    <form  id="album_new_form_id" action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
		<input type="hidden" name="is_submitted" value="1" />
		<input type="hidden" name="redirect" value="<?=$redirect?>" />
		<input id="redirectForm" type="hidden" name="redirectForm" value="" />
		<dt><label class="required">Линк към страницата:</label>
        <?=HTML_BASE?>album/<input id="sef_url"  name="sef_url" type="text" value="<?=$_POST['sef_url']?>" >
        </dt>
        <!-- Tabs beginning-->           
     	<div id="tabs">
        	<ul id="tabs-ui"></ul>
			
   
 		 <?php 
			
			$sql = 'SELECT lang_id,Name,code
					FROM language
					WHERE published';
			$result = query($sql);
			
			$i=0;
			$tabs="";
			while ($row_lang = mysql_fetch_object($result)) {
				$em = $row_lang->lang_id;
				$tabs.='<li><a href="'.$_SERVER['PHP_SELF'].'#tabs-'.$em.'">'.$row_lang->Name.'</a></li>';
				
				eval('$name =  $_POST[\'title_'.$em.'\'];');
				
				  
			?>
            
  			<div id="tabs-<?=$em?>"> 
  					
    				<dt style="float:left;"><br />

      					<label class="required">Заглавие на <?=$row_lang->Name?></label>
					</dt>
                    
    				<dd> 
     					 <input id="title_<?=$row_lang->lang_id?>" class="inputbox w500" name="title_<?=$row_lang->lang_id?>" type="text" value="<?=$name?>" >
					</dd>
                    
					
				
			</div>
		<?php
            $i++;
            }
        ?>
		</div>
        
 		<dl style="float:left; width:45%; margin-right:2%">
   			<dt>
      			<label >Категория</label>
    		</dt>
    		<dd>
				<ul class="type-box">
				<?php         
                    $sql_c = "SELECT
                    c.category_id,
                    d.Name
                    
                FROM
                    category AS c,
                    category_description AS d
                WHERE
                
                    c.category_id = d.category_id
                    
                    AND
                    
                    d.lang_id = '1'
                    
                    AND
                    c.published
                    
                    ORDER BY c.order_number DESC
                    
                  ";
                
                    $result_c = query($sql_c);
                    $i = 1;	
                    while ($row = mysql_fetch_object($result_c)) {
                          ?>
                        <li>
                          <label>
                            <input class="checkbox" name="category_id[<?=$i?>]" type="checkbox" value="<?=$row->category_id?>" 
                                        <?=(($row->category_id == $_POST["category_id"][$i])?"checked=\"checked\"":"")?> />
                            <?=$row->Name?>
                          </label>
                        </li>
        			<?php $i++; } ?>
				</ul>
			</dd>
		</dl>
		
        
        <dl style="float:left; width:48%;">
   			<dt>
      			<label >Фотограф</label>
    		</dt>
    		<dd>
				<ul class="type-box">
				<?php         
                    $sql_c = "SELECT
                    c.photographers_id,
                    d.title
                    
                FROM
                    photographers AS c,
                    photographers_description AS d
                WHERE
                
                    c.photographers_id = d.photographers_id
                    
                    AND
                    
                    d.lang_id = '1'
                    
                    AND
                    c.published
                    
                    ORDER BY c.order_number DESC
                    
                  ";
                
                    $result_c = query($sql_c);
                    $i = 1;	
                    while ($row = mysql_fetch_object($result_c)) {
                          ?>
                        <li>
                          <label>
                            <input class="checkbox" name="photographer_id[<?=$i?>]" type="checkbox" value="<?=$row->photographers_id?>" 
                                        <?=(($row->photographers_id == $_POST["photographer_id"][$i])?"checked=\"checked\"":"")?> />
                            <?=$row->title?>
                          </label>
                        </li>
        			<?php $i++; } ?>
				</ul>
			</dd>
		</dl>
        
        
        
		<dl>	
		
		<label>
			<input class="checkbox" name="published" id="published" type="checkbox" value="1" <?=(($_POST['published'])?"checked=\"checked\"":"")?> />
Публикуван</label>
		</dl>
    
    	<input type="submit" name="submit" id="submit" class="button" value="Създай">
    	
	</form>





<script>

	$(document).ready(function() {
		
		// Tabs
		$("#tabs-ui").html('<?=$tabs?>');
		$( "#tabs" ).tabs();	
		
	});
</script>

<?php
	foot_admin(); 
?>
